package com.gitee.wsl.compose.modifier

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.alpha
import com.gitee.wsl.platform.PlatformType
import com.gitee.wsl.platform.isMobile
import com.gitee.wsl.platform.platform

@Composable
fun Modifier.disabled(disabled: Boolean = true): Modifier =
    if (disabled) this.alpha(0.38f) else this


fun Modifier.conditional(
    condition: Boolean,
    ifFalse: (@Composable Modifier.() -> Modifier)? = null,
    ifTrue: @Composable Modifier.() -> Modifier
): Modifier = composed {
    if (condition) {
        ifTrue.invoke(this)
    } else {
        ifFalse?.invoke(this) ?: this
    }
}


/**
 * Conditionally applies the [action] to the receiver [Modifier], if [precondition] is true. Returns the receiver as-is
 * otherwise.
 */
inline fun Modifier.thenIf(precondition: Boolean,
                           action: Modifier.() -> Modifier): Modifier =
    if (precondition) action() else this


/**
 * Modifier check bool is FALSE for set params
 */
inline fun Modifier.ifFalse(value: Boolean,
                            crossinline block: Modifier.() -> Modifier) =
    then(if (!value) block.invoke(this) else this)

/**
 * Modifier check bool is TRUE for set params
 */
inline fun Modifier.ifTrue(value: Boolean,
                           crossinline block: Modifier.() -> Modifier) =
    then(if (value) block.invoke(this) else this)

/**
 * Calls the given [callback] only if [obj] is not `null`.
 */
inline fun <T : Any> Modifier.ifNotNull(obj: T?,
                                        callback: Modifier.(T) -> Modifier): Modifier {
    return if (obj == null) {
        this
    } else {
        callback(obj)
    }
}

/**
 * Apply the [ifTrue] block to the modifier chain only if the [condition] is `true`, otherwise apply [ifFalse]
 * (nothing additional by default)
 *
 * @return the continued modifier chain with changes applied, if any.
 */
inline fun Modifier.thenIf(
    condition: Boolean,
    ifFalse: Modifier.() -> Modifier = { this },
    ifTrue: Modifier.() -> Modifier
): Modifier = then(Modifier.let { if (condition) it.ifTrue() else it.ifFalse() })

/**
 * Apply the  [block] to the modifier chain only if the [value] is not null, otherwise apply [ifNull]
 * (nothing additional by default)
 *
 * @return the continued modifier chain with changes applied, if any.
 */
 inline fun <T> Modifier.thenIfNotNull(
    value: T,
    ifNull: Modifier.() -> Modifier = { this },
    block: Modifier.(T & Any) -> Modifier
): Modifier = then(Modifier.let { if (value != null) it.block(value) else it.ifNull() })

internal val LocalPlatform get() = platform.current

/**
 * Modifiers for different platforms depending on the platform
 */
fun Modifier.platform(
    vararg platforms: PlatformType,
    modifier: Modifier.() -> Modifier
): Modifier {
    if (platforms.isEmpty()) return this
    return if (platforms.any { it == LocalPlatform })
        this.modifier()
    else
        this
}